/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

package org.apache.directory.studio.persistence.core;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum;
import org.apache.directory.shared.ldap.schema.parsers.AttributeTypeDescription;
import org.apache.directory.shared.ldap.schema.parsers.ObjectClassDescription;
import org.apache.directory.studio.ldapbrowser.core.model.schema.Schema;
import org.apache.directory.studio.ldapbrowser.core.model.schema.SchemaUtils;


/**
 * This is only a temporary utils class, should be merged into 
 * org.apache.directory.studio.ldapbrowser.core.model.schema.SchemaUtils.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class SchemaUtils2
{

    private SchemaUtils2()
    {

    }


    /**
     * Gets the structural object class from a given set of object
     * class descriptions and a Schema
     *
     * @param ocds the object class descriptions
     * @param schema the schema
     * @return structuralObjectClass the structural object class
     *
     * @throws IllegalArgumentException if @param ocds is empty or null or does not
     * contain object classes which are structural and if the selected structural object
     * is not in the same super class tree hierarchy as the super classes -this happens 
     * when the object class description list contains values which are not in the same 
     * superclass chain 
     */
    public static ObjectClassDescription getStructuralObjectClass( Collection<ObjectClassDescription> ocds, Schema schema )
        throws IllegalArgumentException
    {
        if ( ocds == null || ocds.isEmpty() )
        {
            throw new IllegalArgumentException();
        }
        List<ObjectClassDescription> strucObjClasses = new ArrayList<ObjectClassDescription>();
        List<ObjectClassDescription> superiorObjClasses = new ArrayList<ObjectClassDescription>();
        ObjectClassDescription specificObjectClass;
        for ( ObjectClassDescription objClassDesc : ocds )
        {
            if ( objClassDesc.getKind().equals( ObjectClassTypeEnum.STRUCTURAL ) )
            {
                strucObjClasses.add( objClassDesc );
            }
            superiorObjClasses.addAll( SchemaUtils.getSuperiorObjectClassDescriptions( objClassDesc, schema ) );
        }

        if ( strucObjClasses.isEmpty() )
        {
            throw new IllegalArgumentException();
        }

        for ( ObjectClassDescription objClass : superiorObjClasses )
        {
            if ( strucObjClasses.contains( objClass ) )
            {
                strucObjClasses.remove( objClass );
            }
        }

        specificObjectClass = strucObjClasses.get( 0 );
        strucObjClasses.remove( 0 );
        if ( specificObjectClass.getSuperiorObjectClasses().containsAll( strucObjClasses ) )
        {
            return specificObjectClass;
        }
        else
        {
            throw new IllegalArgumentException();
        }

    }


    /**
     * Gets the attributes of all given object class descriptions including all
     * attributes of all superior object class descriptions (transitively).
     *
     * @param ocds the object class descriptions
     * @param schema the schema 
     * @return the list of all attributes
     */
    public static Collection<String> getAllAttributes( Collection<ObjectClassDescription> ocds, Schema schema )
    {
        // Use a hash set here because the same attribute can be defined in different object classes
        // and we don't want duplicate attributes.
        Collection<String> attributes = new HashSet<String>();

        for ( ObjectClassDescription ocd : ocds )
        {
            Collection<String> mayAttributes = SchemaUtils.getMayAttributeTypeDescriptionNamesTransitive( ocd, schema );
            Collection<String> mustAttributes = SchemaUtils
                .getMustAttributeTypeDescriptionNamesTransitive( ocd, schema );
            attributes.addAll( mayAttributes );
            attributes.addAll( mustAttributes );
        }

        return attributes;
    }


    /**
     * Checks if the given attribute type is defined as MUST attribute in one of the 
     * object classes.
     *
     * @param atd the attribute type description
     * @param ocds the object class descriptions
     * @param schema the schema
     * @return true if the attribute is defined as MUST in one of the object classes
     */
    public static boolean isMustAttribute( AttributeTypeDescription atd, Collection<ObjectClassDescription> ocds,
        Schema schema )
    {
        if ( atd == null || ocds == null || schema == null )
        {
            throw new IllegalArgumentException();
        }

        for ( ObjectClassDescription ocd : ocds )
        {
            Collection<String> musts = SchemaUtils.getMustAttributeTypeDescriptionNamesTransitive( ocd, schema );
            for ( String must : musts )
            {
                AttributeTypeDescription mustAtd = schema.getAttributeTypeDescription( must );
                if ( atd.getNumericOid().equals( mustAtd.getNumericOid() ) )
                {
                    return true;
                }
            }
        }

        return false;
    }

}
